package com.kyhd.audio.controller;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.kyhd.audio.dao.model.AudioExchangeLog;
import com.kyhd.audio.dao.model.Novel;
import com.kyhd.audio.service.AudioExchangeLogService;
import com.kyhd.audio.service.AudioExchangeService;
import com.kyhd.audio.service.NovelService;
import com.kyhd.audio.util.InitConst;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@Api("音频管理接口")
@RestController
@Slf4j
@RequestMapping(value = "/audio/api", produces = { "application/json" })
public class AudioController {

	@Autowired
	NovelService novelService;
	
	@Autowired
	AudioExchangeLogService audioExchangeLogService;
	
	@Autowired
	AudioExchangeService audioExchangeService;
	
	@ApiOperation(value = "获取音频文件", notes = "")
	@RequestMapping(method = { RequestMethod.GET }, path = "/getaudio")
	public @ResponseBody ResponseEntity<String> getAudioPath(
			@ApiParam(value = "小说id") @RequestParam(name = "novelid", required = true) String novelId,
			@ApiParam(value = "章节内容id") @RequestParam(name = "contentid", required = true) String contentId,
			@ApiParam(value = "时间戳") @RequestParam(name = "times", required = true) String times,
			@ApiParam(value = "token") @RequestParam(name = "token", required = true) String token
			) {
		HttpHeaders headers = new HttpHeaders();
		headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList("application/json;charset=UTF-8"));
		JSONObject ret = new JSONObject();
		try {
			String myToken = this.audioExchangeService.createToken(times, "MD5");
			if(!token.equals(myToken)) {
				ret.put("code", InitConst.API_STATUS_FAILED);
				ret.put("msg", "token error");
				return new ResponseEntity<String>(ret.toString(), headers, HttpStatus.OK);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("code", InitConst.API_STATUS_FAILED);
			ret.put("msg", "token error");
			return new ResponseEntity<String>(ret.toString(), headers, HttpStatus.OK);
		}
		Novel novel = this.novelService.getByNovelIdAndContentId(novelId, contentId);
		String msg = "";
		if(novel!=null) {
			if(novel.getAudio()==1) {
				AudioExchangeLog audioExchangeLog = this.audioExchangeLogService.getByNovelIdAndContentId(novelId, contentId);
				if(audioExchangeLog!=null&&audioExchangeLog.getPath()!=null&&!"".equals(audioExchangeLog.getPath())) {
					ret.put("code", InitConst.API_STATUS_SUCCESS);
					ret.put("msg", "success");
					ret.put("novelId", novelId);
					ret.put("contentId", contentId);
					ret.put("path", audioExchangeLog.getPath());
					log.info("novelId:{},contentId:{},path:{}",novelId,contentId,audioExchangeLog.getPath());
					return new ResponseEntity<String>(ret.toString(), headers, HttpStatus.OK);
				}else {
					msg = "This novel chapter has not path.";
				}
			}else {
				msg = "This novel chapter has not exchange.";
			}
		}else {
			msg = "This novel chapter does not exist.";
		}
		ret.put("code", InitConst.API_STATUS_FAILED);
		ret.put("msg", msg);
		ret.put("novelId", novelId);
		ret.put("contentId", contentId);
		log.info("getAudioPath response:{}",ret.toString());
		return new ResponseEntity<String>(ret.toString(), headers, HttpStatus.OK);
	}
	
	@ApiOperation(value = "人工调用音频转换", notes = "")
	@RequestMapping(method = { RequestMethod.GET }, path = "/exchange")
	public @ResponseBody ResponseEntity<String> exchange(
			@ApiParam(value = "小说ids") @RequestParam(name = "novelids", required = true) String novelIds,
			@ApiParam(value = "时间戳") @RequestParam(name = "times", required = true) String times,
			@ApiParam(value = "token") @RequestParam(name = "token", required = true) String token) {
		HttpHeaders headers = new HttpHeaders();
		headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList("application/json;charset=UTF-8"));
		JSONObject ret = new JSONObject();
		try {
			String myToken = this.audioExchangeService.createToken(times, "MD5");
			if(!token.equals(myToken)) {
				ret.put("code", InitConst.API_STATUS_FAILED);
				ret.put("msg", "token error");
				return new ResponseEntity<String>(ret.toString(), headers, HttpStatus.OK);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("code", InitConst.API_STATUS_FAILED);
			ret.put("msg", "token error");
			return new ResponseEntity<String>(ret.toString(), headers, HttpStatus.OK);
		}
		
		this.audioExchangeService.sendMsgByIds(novelIds, true);
		ret.put("code", InitConst.API_STATUS_SUCCESS);
		ret.put("msg", InitConst.API_MSG_SUCCESS);
		return new ResponseEntity<String>(ret.toString(), headers, HttpStatus.OK);
	}
	
}
